package www.bplead.com.mydemo.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.GridView;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

import www.bplead.com.mydemo.R;
import www.bplead.com.mydemo.adapter.ImageGridAdapter;

/**
 * Created by wangyangzi on 2016/10/16.
 */
public class UploadImageActivity extends AppCompatActivity implements View.OnClickListener {
    private static final String TAG = "UploadImageActivity";
    GridView uploadGV;
    Button takePictureBtn;
    Button selAlbumBtn;
    Button uploadImageBtn;
    ImageGridAdapter adapter;
    static final int NONE = 0;
    static final int PHOTOHRAPH = 1;// 拍照
    static final int PHOTOZOOM = 2; // 缩放
    static final int PHOTORESOULT = 3;// 结果
    static final String IMAGE_UNSPECIFIED = "image/*";
    List<Bitmap> bitmapList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.upload_image);

        uploadGV = (GridView) this.findViewById(R.id.gv_upload_image);
        takePictureBtn = (Button) this.findViewById(R.id.btn_take_picture);
        selAlbumBtn = (Button) this.findViewById(R.id.btn_sel_album);
        uploadImageBtn = (Button) this.findViewById(R.id.btn_start_upload);

        takePictureBtn.setOnClickListener(this);
        uploadImageBtn.setOnClickListener(this);
        selAlbumBtn.setOnClickListener(this);

        bitmapList = new ArrayList<>();
        adapter = new ImageGridAdapter(this,bitmapList);
        uploadGV.setAdapter(adapter);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            //拍照获取图片
            case R.id.btn_take_picture:
                Intent intent1 = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                intent1.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(
                        Environment.getExternalStorageDirectory(), "temp.jpg")));
                System.out.println("=============" + Environment.getExternalStorageDirectory());
                startActivityForResult(intent1, PHOTOHRAPH);
                break;
            //相册获取图片
            case R.id.btn_sel_album:
                Intent intent2 = new Intent(Intent.ACTION_GET_CONTENT, null);
                intent2.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                        IMAGE_UNSPECIFIED);
                startActivityForResult(intent2, PHOTOZOOM);
                break;
            //将图片上传至服务器
            case R.id.btn_start_upload:
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == NONE)
            return;
        // 拍照
        if (requestCode == PHOTOHRAPH) {
            // 设置文件保存路径这里放在跟目录下
            File picture = new File(Environment.getExternalStorageDirectory()
                    + "/temp.jpg");
            System.out.println("------------------------" + picture.getPath());
            startPhotoZoom(Uri.fromFile(picture));
        }

        if (data == null)
            return;

        // 读取相册缩放图片
        if (requestCode == PHOTOZOOM) {
            startPhotoZoom(data.getData());
        }
        // 处理结果
        if (requestCode == PHOTORESOULT) {
            Bundle extras = data.getExtras();
            if (extras != null) {
                Bitmap photo = extras.getParcelable("data");
                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                photo.compress(Bitmap.CompressFormat.JPEG, 75, stream);// (0 -
                // 100)压缩文件
                //imageView.setImageBitmap(photo);
                bitmapList.add(photo);
                adapter.notifyDataSetChanged();
            }
        }
    }

    public void startPhotoZoom(Uri uri) {
        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(uri, IMAGE_UNSPECIFIED);
        intent.putExtra("crop", "true");
        // aspectX aspectY 是宽高的比例
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        // outputX outputY 是裁剪图片宽高
        intent.putExtra("outputX", 64);
        intent.putExtra("outputY", 64);
        intent.putExtra("return-data", true);
        startActivityForResult(intent, PHOTORESOULT);
    }
}
